
    function login() {
    // 获取用户名和密码
    let username = document.getElementById("username").value;
    console.log(1);
    console.log(username);
    let password = document.getElementById("password").value;
        // 检查用户名和密码是否为空
        if (username === '' || password === '') {
            alert('用户名和密码不能为空！');

        }

    // 创建一个XMLHttpRequest对象
    let xhr = new XMLHttpRequest();


    // 设置POST请求，将数据发送到指定的URL
    xhr.open("POST", "login", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    // 设置回调函数，处理服务器响应
    xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && xhr.status == 200) {
    // 服务器返回的数据
    let response = xhr.responseText;
        // 处理后端返回的响应

        if (response.status === 'success') {
            window.location.href = "/main";
        } else if (response.status === 'user_not_found') {
            alert('该用户不存在！');
        } else if (response.status === 'incorrect_password') {
            alert('密码错误！');
        }

}
    else {
        alert("请求错误！");
    }

};

        // 构造请求体数据
        let data = "username=" + encodeURIComponent(username) + "&password=" + encodeURIComponent(password);
        console.log(username);
        // 发送请求
        xhr.send(data);

    };


